import { Sprite } from 'pixi.js';
import BaseSprite from './BaseSprite.js';
import ResourceManager from '../resource.js';

class GroundSprite extends BaseSprite {
    constructor(app) {
        super(app, 'ground');
        
        this.pixel = 18;
        this.stones = [];
        
        this.resources = {
            stone: './stone.png'
        };
    }

    async loadResources() {
        for (const [name, url] of Object.entries(this.resources)) {
            await ResourceManager.load(name, url);
        }
    }

    async init() {
        const texture = ResourceManager.get('stone');

        for (let y = 640; y >= 640 - 72; y -= this.pixel) {
            for (let x = -4; x < 640; x += this.pixel) {
                const stone = new Sprite(texture);
                stone.x = x;
                stone.y = y;
                this.addSprite(stone);
                this.stones.push(stone);
            }
        }
    }

    getGroundY() {
        return 640 - 64 * 2 - 72;
    }

    destroy() {
        this.stones = [];
        super.destroy();
    }
}

export default GroundSprite;
